package org.lgiavedoni.android.franq.event.generation.phone;

import org.lgiavedoni.android.franq.core.listener.PhoneStateListener;
import org.lgiavedoni.android.franq.event.CallFromEvent;
import org.lgiavedoni.android.franq.service.event.EventService;
import org.lgiavedoni.android.franq.service.log.Logger;
import org.lgiavedoni.android.franq.service.phone.listener.PhoneListenerService;

import android.telephony.TelephonyManager;

public class CallFromListener extends PhoneStateListener {

	@Override
	public int getEventListener() {
		return PhoneStateListener.LISTEN_CALL_STATE;
	}

	@Override
	public final void onCallStateChanged(int state, String incomingNumber) {
		if (PhoneListenerService.inst.isStarted()) {
			run(state, incomingNumber);
		}
	}

	public void run(int state, String incomingNumber) {
		switch (state) {
		case TelephonyManager.CALL_STATE_IDLE:
			Logger.inst.debug("IDLE");
			break;
		case TelephonyManager.CALL_STATE_OFFHOOK:
			Logger.inst.debug("OFFHOOK");
			break;
		case TelephonyManager.CALL_STATE_RINGING: {
			Logger.inst.info("RINGING");
			CallFromEvent e = new CallFromEvent(incomingNumber);
			EventService.inst.fireEvent(e);
			break;
		}
		}
	}

}
